<template>
	<view class="join-page">
		<u-navbar title="门店入驻" :autoBack="true" :placeholder="true" bgColor="rgba(255,255,255,0)"></u-navbar>
		<view style="height: 260rpx;"></view>
		<view class="content" v-if="info == null">
			<view class="info-item">
				<view class="item-title">
					店铺名称
				</view>
				<view class="info-right">
					<view class="item-text">
						<u-input v-model="shop_name" placeholder="请输入店铺名称" border="none" inputAlign="right"></u-input>
					</view>
				</view>
			</view>
			<view class="info-item" @click="show = true">
				<view class="item-title">
					门店分类
				</view>
				<view class="info-right">
					<view class="item-text">
						<u-input v-model="shop_classify" placeholder="请选择门店分类" border="none"
							inputAlign="right"></u-input>

					</view>
					<u-icon name="arrow-down" color="#8B8DA0" size="20"></u-icon>
				</view>
			</view>
			<view class="provide">
				<view class="provide-title">
					门店提供
				</view>
				<view class="provide-box">
					<view :class="item.show == true?'activity-item':''" class="provide-item"
						v-for="(item,index) in provideList" :key="index" @click="cutProvide(index)">
						{{item.text}}
					</view>
				</view>
			</view>
			<view class="info-item">
				<view class="item-title">
					详细地址
				</view>
				<view class="info-right">
					<view class="address">
						{{address}}
					</view>
					<view class="place" @click="goposition">
						<image src="https://m.lqvvvv.com/assets/static/my/place.png" mode=""></image>
					</view>
				</view>
			</view>
			<view class="info-item">
				<view class="item-title">
					营业时间
				</view>
				<view class="info-right" @click="$refs.timeslot.open()">
					<view class="item-text">
						<u-input v-model="time" placeholder="8:00-20:00" border="none" inputAlign="right"></u-input>
					</view>
					<u-icon name="arrow-right" color="#8B8DA0" size="20"></u-icon>
				</view>
			</view>
			<view class="info-item">
				<view class="item-title">
					联系电话
				</view>
				<view class="info-right">
					<view class="item-text">
						<u-input v-model="tell" placeholder="请输入联系电话" border="none" type="number" maxlength="20"
							inputAlign="right"></u-input>
					</view>
				</view>
			</view>
			<view class="provide">
				<view class="provide-title">
					店铺简介
				</view>
				<view class="textarea">
					<u--textarea class="" border="none" v-model="brief" placeholder="请输入店铺简介"></u--textarea>
				</view>
			</view>
			<view class="title">
				店铺头像
			</view>
			<view class="upload-box">
				<u-upload :fileList="fileList1" width="179rpx" height="179rpx" @afterRead="afterRead"
					@delete="deletePic" name="1" multiple :maxCount="1" uploadIconColor="#c6cafa"></u-upload>
			</view>
			<view class="title">
				店铺图片<text style="color: red;">(可上传多张相片)</text>
			</view>
			<view class="upload-box">
				<u-upload :fileList="fileList2" width="179rpx" height="179rpx" @afterRead="afterRead"
					@delete="deletePic" name="2" multiple :maxCount="9" uploadIconColor="#c6cafa"></u-upload>
			</view>
			<view class="title">
				店铺视频
			</view>
			<view class="upload-item" v-if="video==''" @click="goupvideo">
				<image src="https://m.lqvvvv.com/assets/static/my/upicon2.png" mode=""></image>
			</view>
			<video style="width: 179rpx; height: 179rpx;" :controls="false" v-if="video!=''"
				:src="'https://m.lqvvvv.com'+video"></video>
			<view style="width: 179rpx;" v-if="video!=''" @click="video = ''">
				<u-button type="primary" color="#5755F2" size="mini" text="重新上传"></u-button>
			</view>
			<view class="btn">
				<u-button @click="goup" color="linear-gradient( 90deg, #C58AF0 0%, #6272ED 100%)" type="primary"
					text="确认提交" size="large" shape="circle"></u-button>
			</view>
			<u-picker :show="show" keyName="name" :columns="columns" :defaultIndex="defaultIndex" @cancel="show = false"
				@confirm="confirm"></u-picker>
		</view>
		<view v-if="info!=null">
			<view class="icon2" v-if="info.approve_status == 0">
				<image src="https://m.lqvvvv.com/assets/static/my/tisp.png" mode=""></image>
				<view class="title">审核中...</view>
			</view>
			<view class="icon3" v-if="info.approve_status == 1">
				<image src="https://m.lqvvvv.com/assets/static/my/scess.png" mode=""></image>
				<view class="title">审核已通过</view>
			</view>
			<view class="icon" v-if="info.approve_status == 2">
				<image src="https://m.lqvvvv.com/assets/static/my/no.png" mode=""></image>
				<view class="title">审核失败</view>
			</view>
			<view style="margin-top: 200rpx; padding: 0 30rpx;">
				<u-button size="large" shape="circle" color="linear-gradient( 90deg, #C58AF0 0%, #6272ED 100%)"
					type="primary" v-if="info.approve_status == 2" @click="gonewup" text="审核失败,重新提交"></u-button>
				<u-button size="large" shape="circle" color="linear-gradient( 90deg, #C58AF0 0%, #6272ED 100%)"
					type="primary" v-if="info.approve_status == 1"
					@click="$navTo.togo('/pages/business/mybusiness/index')" text="前往店铺管理"></u-button>
			</view>
		</view>
		<timeSlot ref="timeslot" :title="'选择时间段'" @confirm="confirmTime">
		</timeSlot>
	</view>
</template>

<script>
	import permision from "@/js_sdk/wa-permission/permission.js";
	import timeSlot from "@/components/wanghexu-timeslot/wanghexu-timeslot.vue"
	export default {
		components: {
			timeSlot
		},
		data() {
			return {
				info: {},
				show: false,
				shop_name: '',
				shop_classify: '',
				time: '',
				tell: '',
				brief: '',
				columns: [],
				address: '',
				lat: '',
				lng: '',
				classifyid: "",
				defaultIndex: [0],
				provideList: [],
				fileList1: [],
				fileList2: [],
				video: '',
				newinfo: {}
			}
		},
		onLoad() {
			this.getCategory()
			this.getconfig()
			this.getApproveStatus()
		},
		methods: {
			confirmTime(e) {
				let params = {
					open_start: e.start.hour + ':' + e.start.min + ':' + '00',
					open_end: e.end.hour + ':' + e.end.min + ':' + '00'
				}
				this.time = params.open_start + '-' + params.open_end
			},
			gonewup() {
				this.info = null
			},
			getApproveStatus() {
				let params = {

				}
				this.$Request({
					method: 'POST',
					url: '/api/user_shop/getApproveStatus',
					data: params,
					ismsg: false,
				}).then(res => {
					if (res.code == 1) {
						console.log(res)
						this.info = res.data
						this.newinfo = res.data
					}
				})
			},
			getconfig() {
				let params = {
					name: 'shop_tags'
				}
				this.$Request({
					method: 'POST',
					url: '/api/index/getConfig',
					data: params,
					ismsg: false,
				}).then(res => {
					if (res.code == 1) {
						console.log(res)
						this.provideList = res.data.split(',').map(item => ({
							text: item.trim(),
							show: false
						}));
					}
				})
			},
			async goup() {
				if (this.shop_name == '') {
					uni.showToast({
						title: '请输入店铺名称！',
						icon: 'none'
					})
					return
				}
				if (this.shop_classify == '') {
					uni.showToast({
						title: '请选择门店分类！',
						icon: 'none'
					})
					return
				}
				if (!this.provideList.some(item => item.show)) {
					uni.showToast({
						title: '请选择门店提供服务！',
						icon: 'none'
					});
					return;
				}
				if (this.address == '') {
					uni.showToast({
						title: '请选择门店地址！',
						icon: 'none'
					})
					return
				}
				if (this.time == '') {
					uni.showToast({
						title: '请输入营业时间！',
						icon: 'none'
					})
					return
				}
				if (this.tell == '') {
					uni.showToast({
						title: '请输入联系电话！',
						icon: 'none'
					})
					return
				}
				if (this.brief == '') {
					uni.showToast({
						title: '请输入店铺简介！',
						icon: 'none'
					})
					return
				}
				if (this.fileList1.length < 1) {
					uni.showToast({
						title: '请上传店铺头像！',
						icon: 'none'
					})
					return
				}
				if (this.fileList2.length < 1) {
					uni.showToast({
						title: '请上传店铺图片！',
						icon: 'none'
					})
					return
				}
				if (this.video == '') {
					uni.showToast({
						title: '请上传店铺视频！',
						icon: 'none'
					})
					return
				}
				uni.showLoading({
					mask: true
				})
				let params = {
					category_id: this.classifyid,
					title: this.shop_name,
					avatar: this.fileList1[0].url,
					intro: this.brief,
					photos: '',
					videos: this.video,
					address: this.address,
					mobile: this.tell,
					tags: this.provideList.filter(item => item.show).map(item => item.text).join(', '),
					service_time: this.time,
					lng: this.lat,
					lat: this.lng
				}
				params.photos = this.fileList2.map(item => item.url.replace('https://m.lqvvvv.com', '')).join(',');
				uni.showLoading({
					mask: true,
					title: '提交中...'
				})
				if (this.newinfo != null) {
					this.$Request({
						method: 'POST',
						url: '/api/user_shop/updateInfo',
						data: params,
						ismsg: true,
					}).then(res => {
						if (res.code == 1) {
							console.log(res)
							uni.navigateBack({
								delta: 1
							})
						}
					})
				} else {
					this.$Request({
						method: 'POST',
						url: '/api/user_shop/apply',
						data: params,
						ismsg: true,
					}).then(res => {
						if (res.code == 1) {
							console.log(res)
							uni.navigateBack({
								delta: 1
							})
						}
					})
				}
			},
			goupvideo() {
				let app = this
				uni.chooseVideo({
					sourceType: ['camera', 'album'],
					success: function(res) {
						console.log(res.tempFilePath)
						uni.showLoading({
							title: '上传中...',
							mask: true
						})
						uni.uploadFile({
							url: 'https://m.lqvvvv.com/api/common/upload', // 仅为示例，非真实的接口地址
							filePath: res.tempFilePath,
							name: 'file',
							header: {
								token: uni.getStorageSync("token") || '',
							},
							success: (res1) => {
								uni.hideLoading()
								let info = JSON.parse(res1.data)
								if (info.code == 1) {
									app.video = info.data.url
								} else {
									uni.showToast({
										title: info.msg,
										icon: 'none'
									})
								}
							}
						});
					}
				});
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'https://m.lqvvvv.com/api/common/upload',
						filePath: url,
						name: 'file',
						header: {
							token: uni.getStorageSync("token") || '',
						},
						success: (res) => {
							let info = JSON.parse(res.data)
							if (info.code == 1) {
								console.log(info)
								setTimeout(() => {
									resolve(info.data.url)
								}, 1000)
							} else {
								uni.showToast({
									title: info.msg,
									icon: 'none'
								})
							}
						}
					});
				})
			},
			async goposition() {
				let app = this
				// #ifdef APP
				let result = await permision.requestAndroidPermission('android.permission.ACCESS_FINE_LOCATION')
				if (result == 1) {
					uni.chooseLocation({
						success: function(res) {
							app.address = res.address
							app.lat = res.latitude
							app.lng = res.longitude
						}
					});
				} else if (result == 0) {
					uni.showToast({
						title: '请您授权位置权限!方便我们获取您的位置!',
						icon: 'none'
					})
				} else {
					uni.showToast({
						title: '请您打开位置权限！',
						icon: 'none'
					})
					setTimeout(() => {
						permision.gotoAppPermissionSetting()
					}, 1000)
				}
				// #endif
				// #ifdef H5
				uni.chooseLocation({
					success: function(res) {
						app.address = res.address
						app.lat = res.latitude
						app.lng = res.longitude
					}
				});
				// #endif
				// #ifdef MP-WEIXIN	
				uni.chooseLocation({
					success: function(res) {
						app.address = res.address
						app.lat = res.latitude
						app.lng = res.longitude
					}
				});
				// #endif
			},
			getCategory() {
				this.$Request({
					method: 'POST',
					url: '/api/user_shop/getCategory',
					data: {},
					ismsg: false,
				}).then(res => {
					if (res.code == 1) {
						this.columns.push(res.data)
						console.log(this.columns)
					}
				})
			},
			openList() {

			},
			confirm(e) {
				this.shop_classify = e.value[0].name
				this.classifyid = e.value[0].id
				this.show = false
			},
			cutProvide(index) {
				this.provideList[index].show = !this.provideList[index].show
			}
		}
	}
</script>

<style>
	.u-textarea {
		background-color: rgba(0, 0, 0, 0.0);
	}
</style>
<style lang="scss" scoped>
	.join-page {
		min-height: 100vh;
		background-image: url('https://m.lqvvvv.com/assets/static/business/businessbg.png');
		background-repeat: no-repeat;
		background-size: 100% 370rpx;
		background-color: #F7F8FC;

		.icon {
			width: 232rpx;
			height: 249rpx;
			margin: 0 auto;
			margin-top: 160rpx;

			.title {
				text-align: center;
				margin-top: 20rpx;
			}

			image {
				width: 232rpx;
				height: 249rpx;
			}
		}

		.icon2 {
			width: 186rpx;
			height: 199rpx;
			margin: 0 auto;
			margin-top: 160rpx;

			.title {
				text-align: center;
				margin-top: 20rpx;
			}

			image {
				width: 186rpx;
				height: 199rpx;
			}
		}

		.icon3 {
			width: 202rpx;
			height: 202rpx;
			margin: 0 auto;
			margin-top: 160rpx;

			image {
				width: 202rpx;
				height: 202rpx;
			}

			.title {
				text-align: center;
				margin-top: 20rpx;
			}
		}

		.content {
			padding: 30rpx;
			background-color: #fff;
			border-top-right-radius: 30rpx;
			border-top-left-radius: 30rpx;

			.upload-item {
				width: 179rpx;
				height: 179rpx;
				background-color: #F7F8FC;
				display: flex;
				justify-content: center;
				align-items: center;
				border-radius: 10rpx;
				margin: 20rpx 0;

				image {
					width: 55rpx;
					height: 53.58rpx;
				}
			}

			.btn {
				margin-top: 60rpx;
			}

			.info-item {
				background-color: #F7F8FC;
				padding: 30rpx;
				border-radius: 20rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 30rpx;

				.item-title {
					font-size: 28rpx;
					color: #000000;
					font-weight: bold;
				}

				.info-right {
					display: flex;
					align-items: center;

					.address {
						width: 300rpx;
						overflow: hidden;
						/* 确保超出容器的文本被裁剪 */
						white-space: nowrap;
						/* 确保文本在一行内显示 */
						text-overflow: ellipsis;
						/* 使用省略号表示文本超出 */
					}

					.item-text {
						margin-right: 20rpx;
						font-size: 24rpx;
						color: #999;
					}

					.place {
						color: #5755F2;
						font-size: 28rpx;
						display: flex;
						align-items: center;

						image {
							width: 30rpx;
							height: 33rpx;
							margin-right: 10rpx;
						}
					}
				}

				.right-input {
					display: flex;
					align-items: center;

					.time-tips {
						font-size: 28rpx;
						color: #b8b8b8;
					}

					image {
						width: 32rpx;
						height: 32rpx;
					}
				}
			}

			.provide {
				background-color: #F7F8FC;
				padding: 30rpx;
				border-radius: 20rpx;
				margin-bottom: 30rpx;

				.textarea {
					margin-top: 30rpx;
				}

				.provide-title {
					font-size: 28rpx;
					color: #000000;
					font-weight: bold;
				}

				.provide-box {
					display: flex;
					flex-wrap: wrap;

					.provide-item {
						padding: 10rpx 16rpx;
						border-radius: 20rpx;
						background-color: #ffffff;
						font-size: 24rpx;
						color: #8B8DA0;
						margin: 30rpx 20rpx 0 0;
					}

					.activity-item {
						background-color: #5755F2;
						color: #ffffff;
					}
				}

			}

			.title {
				padding-bottom: 30rpx;
				color: #000000;
				font-size: 28rpx;
				font-weight: bold;
			}
		}
	}
</style>